package com.xyhuo.leetcode.december;

/**
 * 搜索插入位置
 * https://leetcode.cn/problems/search-insert-position/
 *
 * @time 2023/12/13
 */
public class SearchInsert {

//    public static void main(String[] args) {
//        SearchInsert si = new SearchInsert();
//        int[] nums = {1, 3, 5, 6};
//        si.searchInsert(nums, 2);
//    }

    public int searchInsert(int[] nums, int target) {
        int len = nums.length, left = 0, right = len - 1;
        while (left <= right) {
            int middle = (left + right) / 2;
            if (nums[middle] == target) {
                return middle;
            } else if (nums[middle] < target) {
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }
        return left;
    }
}
